package com.admin.business.voteVotingSettings;

import com.alibaba.druid.util.StringUtils;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 首次成立业主大会投票设置
 * @create: 2022-07-08 19:04:49
 **/
@Service("voteVotingSettings")
@Slf4j
public class VoteVotingSettingsService {

    @Resource
    VoteVotingSettingsDao voteVotingSettingsDao;

    /**
     * @Author Zwl
     * @Description //TODO 通过省级id获取市级投票信息
     * @Date 2022-11-18 15:17:21
     * @Param cdoRequest
     * @return com.tiangong.framework.bo.Return
     */
    @OpenApi
    public Return getVoteVotingSettingsByParentId(CDO cdoRequest) {
        if (StringUtils.isEmpty(cdoRequest.getString("parent_id"))){
            return Return.valueOf(-1,"请选择省级地区");
        }
        List<CDO> getVoteVotingSettingsByParentId = voteVotingSettingsDao.getVoteVotingSettingsByParentId(cdoRequest);
        for (CDO cdo : getVoteVotingSettingsByParentId) {
            String community_province = cdo.getString("community_city");
            if (community_province != null) {
                CDO addressInfo = new CDO();
                addressInfo.set("community_city", community_province);
                addressInfo.set("essential_information_id", 0);//只查信息公司设置的
                CDO voteVotingSettings = voteVotingSettingsDao.getVoteVotingSettings(addressInfo);
                cdo.set("voteSet", voteVotingSettings);
                cdo.set("discoloration", true);//变色
            } else {
                cdo.set("discoloration", false);//不变色
            }
        }
        CDO cdoResult = new CDO();
        cdoResult.set("datalist", getVoteVotingSettingsByParentId);
        return Return.valueOf(cdoResult);
    }

    //    业主大会设置页面查询市区
    @OpenApi
    public Return getVoteAddressInformationSetUp(CDO cdoRequest) {
        List<CDO> voteAddressInformationSetUp = voteVotingSettingsDao.getVoteAddressInformationSetUp(cdoRequest);

        for (CDO setInfo : voteAddressInformationSetUp) {
            String community_province = setInfo.getString("community_province");
            if (community_province != null) {
                CDO addressInfo = new CDO();
                addressInfo.set("community_province", community_province);
                addressInfo.set("essential_information_id", 0);//只查信息公司设置的
                CDO voteVotingSettings = voteVotingSettingsDao.getVoteVotingSettings(addressInfo);
                setInfo.set("voteSet", voteVotingSettings);
                setInfo.set("discoloration", true);//变色
            } else {
                setInfo.set("discoloration", false);//不变色
            }
        }
        CDO cdoResult = new CDO();
        cdoResult.set("datalist", voteAddressInformationSetUp);
        return Return.valueOf(cdoResult);
    }


    /**
     * 添加首次成立业主大会投票设置,获取id数据
     *
     * @param cdoRequest - id:
     *                   - user_id:用户id
     *                   - essential_information_id:基本信息id
     *                   - community_province:省
     *                   - community_city:市
     *                   - community_area:区/县
     *                   - reconsideration_households:需要附议户数：户数/百分比
     *                   - reconsideration_households_state:需要附议户数：户数/百分比 ：0/1
     *                   - reconsideration_area:需要附议面积：户数/百分比
     *                   - reconsideration_area_state:需要附议面积：户数/百分比  ：0/1
     *                   - begin_time:创建时间
     *                   - update_time:更新时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return addVoteVotingSettings(CDO cdoRequest) {
        if (StringUtils.isEmpty(cdoRequest.getString("community_province"))&&StringUtils.isEmpty(cdoRequest.getString("community_city"))){
            return Return.valueOf(-1, "请选择省级地区或者是市级地区");
        }
        // 防止前端没卡住
        if (cdoRequest.getString("community_province")!=null&&cdoRequest.getString("community_city")!=null){
            return Return.valueOf(-1, "省级地区和市级地区之能选择一个");
        }

        //每个投票设置表只有一个基本信息
        CDO countCha = new CDO();
        countCha.set("essential_information_id", cdoRequest.getString("essential_information_id"));
        countCha.set("community_province", cdoRequest.getString("community_province"));//加个条件
        countCha.set("community_city", cdoRequest.getString("community_city"));//加个条件  //2022-11-18 增加市级投票设置  zwl
        countCha.set("state", 0);
        int voteVotingSetCount = voteVotingSettingsDao.getVoteVotingSettingsCount(countCha);
        if (voteVotingSetCount == 0) {
            voteVotingSettingsDao.addVoteVotingSettings(cdoRequest);
        } else {
            // 现在有就修改信息 不然查询东西太多了
//            获取id 修改信息
            CDO voteVotingSettings = voteVotingSettingsDao.getVoteVotingSettings(countCha);
            if (voteVotingSettings == null) {
                return Return.valueOf(-2, "未查询到改信息");
            }
            cdoRequest.set("id", voteVotingSettings.getString("id"));

            int i = voteVotingSettingsDao.updateVoteVotingSettingsById(cdoRequest);
            if (i < 0) {
                return Return.valueOf(-1, "修改失败");
            }
        }

        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新首次成立业主大会投票设置数据
     *
     * @param cdoRequest - id:
     *                   - user_id:用户id
     *                   - essential_information_id:基本信息id
     *                   - community_province:省
     *                   - community_city:市
     *                   - community_area:区/县
     *                   - reconsideration_households:需要附议户数：户数/百分比
     *                   - reconsideration_households_state:需要附议户数：户数/百分比 ：0/1
     *                   - reconsideration_area:需要附议面积：户数/百分比
     *                   - reconsideration_area_state:需要附议面积：户数/百分比  ：0/1
     *                   - begin_time:创建时间
     *                   - update_time:更新时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return updateVoteVotingSettingsById(CDO cdoRequest) {
        int affectedRows = voteVotingSettingsDao.updateVoteVotingSettingsById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除首次成立业主大会投票设置数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteVotingSettingsById(CDO cdoRequest) {
        int affectedRows = voteVotingSettingsDao.deleteVoteVotingSettingsById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除首次成立业主大会投票设置数据
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteVotingSettings(CDO cdoRequest) {
        int affectedRows = voteVotingSettingsDao.deleteVoteVotingSettings(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取首次成立业主大会投票设置数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingSettingsById(CDO cdoRequest) {
        CDO cdoMapperResult = voteVotingSettingsDao.getVoteVotingSettingsById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取首次成立业主大会投票设置数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingSettings(CDO cdoRequest) {
        CDO cdoMapperResult = voteVotingSettingsDao.getVoteVotingSettings(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取首次成立业主大会投票设置数据列表
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingSettingsList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteVotingSettingsDao.getVoteVotingSettingsList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteVotingSettingsDao.getVoteVotingSettingsListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }


    /**
     * 根据混合条件，获取首次成立业主大会投票设置数据列表
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingSettingsListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteVotingSettingsDao.getVoteVotingSettingsListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteVotingSettingsDao.getVoteVotingSettingsListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
